package st.business.tepth.business.user.busnumber.workholder.maintain.work.content.repairwork;

import java.util.List;

import st.business.tepth.base.OnDataConvertListener;
import st.business.tepth.business.bean.PartLocationBean;
import st.business.tepth.business.bean.PostDiagnoseWrapperBean;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-04-11 11:35
 */

public class RepairWorkPresenter extends RepairWorkContract.Presenter {
    private final RepairWorkContract.View mView;
    private final RepairWorkContract.Model mModel;

    public RepairWorkPresenter(RepairWorkContract.View view) {
        mView = view;
        mModel = new RepairWorkModel();
    }

    @Override
    void deleteDiagnose(String token, int repairId, int faultDiagnosticId,
                        int faultDiagnosticIdType) {
        addDisposable(mModel.deleteDiagnose(token, repairId, faultDiagnosticId,
                faultDiagnosticIdType,
                new OnDataConvertListener<String>() {
                    @Override
                    public void convert(String workBean) {
                        mView.deleteDiagnoseSucceed();
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void confirmDiagnoseList(String token, int repairId, int repairTaskId, int vehicleSeriesId, String faultDiagnosticList) {
        addDisposable(mModel.postDiagnoseList(token, repairId, repairTaskId, vehicleSeriesId,
                faultDiagnosticList,
                new OnDataConvertListener<PostDiagnoseWrapperBean>() {
                    @Override
                    public void convert(PostDiagnoseWrapperBean workBean) {
                        if (workBean != null) {
                            mView.postDiagnoseList(workBean.getRepairDiagnosticList());
                        } else {
                            mView.showError("数据错误");
                        }
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void deleteProgram(String token, int repairId, int faultDiagnosticId,
                       int planId, final int position) {
        addDisposable(mModel.deleteProgram(token, repairId, faultDiagnosticId, planId,
                new OnDataConvertListener<String>() {
                    @Override
                    public void convert(String workBean) {
                        mView.deleteProgramSucceed(position);
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }


    @Override
    void returnGoods(String token, String items, int planId,
                     final int position) {
        addDisposable(mModel.returnGoods(token, items, planId,
                new OnDataConvertListener<String>() {
                    @Override
                    public void convert(String s) {
                        mView.returnGoodsSucceed(position);
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void addCoWorker(String token, int id, final int position) {
        addDisposable(mModel.addCoWorker(token, id,
                new OnDataConvertListener<String>() {
                    @Override
                    public void convert(String s) {
                        mView.addCoWorkerSucceed(position);
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void cancelCoWorker(String token, int id, final int position) {
        addDisposable(mModel.cancelCoWorker(token, id,
                new OnDataConvertListener<String>() {
                    @Override
                    public void convert(String s) {
                        mView.cancelCoWorkerSucceed(position);
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void addTempLocation(String token, final int position,
                         final PartLocationBean partLocationBean, String tempLocation) {
        addDisposable(mModel.addTempLocation(token, tempLocation,
                new OnDataConvertListener<List<PartLocationBean>>() {
                    @Override
                    public void convert(List<PartLocationBean> locationBeans) {
                        PartLocationBean partLocationBeanNew = partLocationBean;
                        if (partLocationBean == null) {
                            partLocationBeanNew = new PartLocationBean();
                        }
                        join(locationBeans, partLocationBeanNew);
                        mView.showTempLocation(partLocationBeanNew, position);
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    private void join(List<PartLocationBean> list, PartLocationBean newP) {
        if (list != null) {
            StringBuilder name = new StringBuilder(newP.getPositionName() == null ? "" : newP.getPositionName() + ",");
            StringBuilder code = new StringBuilder(newP.getPositionCode() == null ? "" : newP.getPositionCode() + ",");
            for (PartLocationBean partLocationBean : list) {
                name.append(partLocationBean.getPositionName()).append(",");
                code.append(partLocationBean.getPositionCode()).append(",");
            }
            name = new StringBuilder(name.substring(0, name.length() - 1));
            code = new StringBuilder(code.substring(0, code.length() - 1));
            newP.setPositionName(name.toString());
            newP.setPositionCode(code.toString());
        }
    }
}
